<?php
class Post extends AppModel{
	
	public $validate = array(
		'name' => array(
			'rule'     => 'notEmpty',
			'required' => true,
			'message' => 'Name field is required.'
		)
	);
	
	function set_menu($datas,$base_url){
		$total_menu_item = count($datas);
		
		foreach($datas as $data){
			if($data['menus']['parent_id']==0){
			
				$id = $data['menus']['id'];
				$title = $data['menus']['title'];
				$url=$data['menus']['url'];
				if(empty($url)){
					$url = $base_url."/Posts/".$title;
				}
				
				$output1.='<li><a href="'.$url.'"><span>'.$title.'</span></a>';
				$output2="";
				foreach($datas as $sub_data){
					$sub_id= $sub_data['menus']['id'];
					$sub_title= $sub_data['menus']['title'];
					$sub_url=$sub_data['menus']['url'];
					
					if(empty($sub_url)){
						$sub_url = $base_url."/Posts/".$sub_title;
					}
					
					if($id==$sub_data['menus']['parent_id']){
						$output2.='<li><a href="'.$sub_url.'">'.$sub_title.'</a>';
						
						$output3 ="";
						foreach($datas as $ss_data){
							$ss_id= $ss_data['menus']['id'];
							$ss_title= $ss_data['menus']['title'];
							$ss_url=$ss_data['menus']['url'];
							if(empty($ss_url)){
								$ss_url = $base_url."/Posts/".$ss_title;
							}
							
							if($sub_id==$ss_data['menus']['parent_id']){
								$output3.='<li><a href="'.$ss_url.'">'.$ss_title.'</a></li>';
							}
						}
						
						if(!empty($output3)){
							$output2 .='<ul>'.$output3.'</ul>';
						}
						
						$output2 .='</li>';
					}
				}
				
				if(!empty($output2)){
					$output1 .= '<ul>'.$output2.'</ul>';
				}
				
				$output1 .='</li>';
			}
				
		}
		
		if(!empty($output1)){
			$output .='<div id="nav_menu">
							<ul>';
			$output .= $output1;			
			$output .='
							</ul>
						</div>';
		}
		
		return $output;
	}
	
	function set_sub_menu($datas, $parent_id){
		
	}
	
	function get_page($menu_title){
		
		$menu_rows= $this->query("select * from menus where title='".$menu_title."'");
		@$menu_id=$menu_rows[0]['menus']['id'];
		$page=$this->query("select * from pages where menu_id='".$menu_id."'");
		
		return $page;
	}
	
	function set_default(){
		$page=$this->query("select * from pages where default_page='1'");
		return $page;
	}
	
	function display_slide_show($title,$base_url){
		//$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		$active = $this->is_active_slide($title);
		$output = "" ;
		if($active){
			$slide=$this->query("select * from galleries order by id desc");
			$output .= '
				<div id="slide_header">
					<div class="wrap">
						<div id="slide-holder">
							<div id="slide-runner">
			';
			$img_count=0;
			foreach($slide as $slide_image){
				$img_count +=1;
				$output .="<a href=''><img id='slide-img-".$img_count."' src='".$base_url."/app/webroot/upload/banner/".$slide_image['galleries']['image']."' class='slide' alt='' width='900' height='289' /></a>";
			}
			
			$output .='
							<div id="slide-controls">
							 <p id="slide-nav"></p>
						</div>
					</div>
						<!--content featured gallery here -->
				</div>
			';
			$output .='
				<script type="text/javascript">
				if(!window.slider) var slider={};
				slider.data=[
			';
			$sl = 0;
			$total = count($slide);
			foreach($slide as $link){
				$sl ++;
				if($sl == $total){
					$output .= '{"id":"slide-img-'.$sl.'","client":"nature beauty","desc":"add your description here"}';
				}else{
					$output .= '{"id":"slide-img-'.$sl.'","client":"nature beauty","desc":"add your description here"},';
				}
			}
			$output .= '
							];
				</script>
				</div>
			</div><!--/header-->
			';
		}else{
			$output ="";
		}
		return $output;
	}
	
	function is_active_slide($title=null){
		if($title==""){
			$default =$this->query("select title from pages where default_page='1'");
			$title = $default[0]['pages']['title'];
		}
		
		$slide=$this->query("select * from pages where title='".$title."'");
		//print_r($slide);
		if($slide[0]['pages']['display_slide']==1){
			return true;
		}else{
			return false;
		}
	}
	
	function contact_form($base,$title){
		$form = "
			<div class='span8 well' style='margin-left:150px !important; margin-top:20px;'>
				<form action='".$base."/Posts/mail' method='post'>
					<br>
					<div class='input text'>
						<label for='ContactName'>Name</label>
						<input id='ContactName' type='text' name='data[Contact][name]' class='span9'>
					</div>
					<br>
					<div class='input text'>
						<label for='ContactFrom'>From</label>
						<input id='ContactFrom' type='text' name='data[Contact][from]' class='span9'>
					</div>
					<br>
					<div class='input text'>
						<label for='ContactSubject'>Subject</label>
						<input id='ContactSubject' type='text' maxlength='255' name='data[Contact][subject]' class='span9'>
					</div>
					<br>
					<div class='input text'>
						<label for='ContactMessage'>Message</label>
						<textarea id='ContactMessage' name='data[Contact][message]' class='span9'></textarea>
					</div>
					<br>
					<div class='submit'>
						<input type='hidden' name='data[Contact][title]' value='".$title."'>
						<input type='submit' value='Send'>
					</div>
				</form>
			</div>";
		return $form;
	}
	
	function is_contact($id){
		$contact = $this->query("select contact_form from pages where id='".$id."'");
		
		if($contact[0]['pages']['contact_form']==1){
			return true;
		}else{
			return false;
		}
	}
	
	function send_mail($contact){
		//print_r($contact);
		$email = $this->query("select * from profiles");
		$to = $email[0]['profiles']['email'];
		$name = $contact['Contact']['name'];
		$from = $contact['Contact']['from'];
		$subject = $contact['Contact']['subject'];
		$message = $contact['Contact']['message'];
		
		$body ="<html>";
		$body .= "<p>".$message."</p>";
		$body .="</html>";
		
		$headers  = "From: ".$name."<".$from.">\r\n";
		$headers .= "Reply-To: ".$from."\r\n";
		$headers .= "Return-Path: ".$from."\r\n";
		//$headers .= "X-Mailer: Drupal\n";
		$headers .= 'MIME-Version: 1.0' . "\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   
		return mail($to, $subject, $body, $headers); 
	}
	
}

?>